package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.JobregisterForm;
import com.vcaresolution.logistic.pojo.Codetable;
import com.vcaresolution.logistic.pojo.Contact;
import com.vcaresolution.logistic.pojo.Jobregister;
import com.vcaresolution.logistic.pojo.Port;
import com.vcaresolution.logistic.pojo.Voyage;
import com.vcaresolution.logistic.to.DataHolderTO;
import com.vcaresolution.logistic.to.JobregisterTO;
import com.vcaresolution.util.Utils;
//import bsh.util.Util;
//import com.vcaresolution.logistic.pojo.Sector;

@Service
public class JobregisterServiceImpl extends BaseService implements JobregisterService{

	private Jobregister populateJobregisterPojo(JobregisterForm jobregisterForm){
		Jobregister jobregister = new Jobregister();
		BeanUtils.copyProperties(jobregisterForm, jobregister);
		
		return jobregister;
	}
	
	@Override
	@Transactional
	public void add(JobregisterForm jobregisterForm) {
		Jobregister jobregister = populateJobregisterPojo(jobregisterForm);

		jobregister.setCreationadate(Utils.getCurrentDate());
		
		try{
			System.out.println("Add ::: "+jobregister.getJobno());
			
			jobregister.setPortByPodid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPodid()));
			jobregister.setPortByPolid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPolid()));
			jobregister.setPortByPfdid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPfdid()));
			
			jobregister.setVoyageByBkgvslid(getDaoFactory().getUtilityDAO().findById(Voyage.class, jobregisterForm.getBkgvslid()));
			jobregister.setContactByHandovertoid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getEmptypickupid()));
			
			jobregister.setContactByShipperid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getShipperid()));
			jobregister.setContactByBillingtoid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getBillingtoid()));
			jobregister.setContactByBookingbyid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getBookingbyid()));
			jobregister.setContactBySalespersonid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getSalespersonid()));
			jobregister.setContactByCarrierid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getCarrierid()));
			jobregister.setContactByChaid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getChaid()));
			
			jobregister.setCodetableByStuffpointid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getStuffpointid()));
			jobregister.setPortByEmptypickupid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getEmptypickupid()));
			jobregister.setCodetableByCategoryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCategoryid()));
			jobregister.setCodetableByCleranceatid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCleranceatid()));
			jobregister.setCodetableByModeid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getModeid()));
			
			if(!Utils.isEmptyInteger(jobregisterForm.getCodetableclass())){
				jobregister.setCodetableByClass(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCodetableclass()));
			}
			
			jobregister.setCodetableByCargotypeid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCargotypeid()));
			jobregister.setCodetableByAdditionalrequirementsid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getAdditionalrequirementsid()));
		
		
			//getDaoFactory().getUtilityDAO().add(jobregister);
			getDaoFactory().getJobDAO().add(jobregister);
		}
		catch(Exception e){
			e.printStackTrace();
			System.out.println("Exception occurred while adding");
		}
	}

	@Override
	@Transactional
	public void update(JobregisterForm jobregisterForm) {
		Jobregister jobregister = populateJobregisterPojo(jobregisterForm);

		jobregister.setCreationadate(Utils.getCurrentDate());
		
		jobregister.setPortByPodid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPodid()));
		jobregister.setPortByPolid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPolid()));
		jobregister.setPortByPfdid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getPfdid()));
		
		jobregister.setVoyageByBkgvslid(getDaoFactory().getUtilityDAO().findById(Voyage.class, jobregisterForm.getBkgvslid()));
		
		if(!Utils.isEmptyInteger(jobregisterForm.getHandovertoid())){
			jobregister.setContactByHandovertoid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getHandovertoid()));
		}
		
		jobregister.setContactByShipperid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getShipperid()));
		jobregister.setContactByBillingtoid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getBillingtoid()));
		jobregister.setContactByBookingbyid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getBookingbyid()));
		jobregister.setContactBySalespersonid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getSalespersonid()));
		jobregister.setContactByCarrierid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getCarrierid()));
		jobregister.setContactByChaid(getDaoFactory().getUtilityDAO().findById(Contact.class, jobregisterForm.getChaid()));
		
		jobregister.setCodetableByStuffpointid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getStuffpointid()));
		jobregister.setPortByEmptypickupid(getDaoFactory().getUtilityDAO().findById(Port.class, jobregisterForm.getEmptypickupid()));
		jobregister.setCodetableByCategoryid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCategoryid()));
		jobregister.setCodetableByCleranceatid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCleranceatid()));
		jobregister.setCodetableByModeid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getModeid()));
		
		if(!Utils.isEmptyInteger(jobregisterForm.getCodetableclass())){
			jobregister.setCodetableByClass(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCodetableclass()));
		}
		
		jobregister.setCodetableByCargotypeid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getCargotypeid()));
		jobregister.setCodetableByAdditionalrequirementsid(getDaoFactory().getUtilityDAO().findById(Codetable.class, jobregisterForm.getAdditionalrequirementsid()));
		
		getDaoFactory().getUtilityDAO().update(jobregister);
	}

	@Override
	@Transactional
	public void delete(JobregisterForm jobregisterForm) {
		getDaoFactory().getUtilityDAO().delete(populateJobregisterPojo(jobregisterForm));
	}

	@Override
	@Transactional
	public JobregisterTO findById(Serializable jobregisterID) {
		Jobregister jobregister = (Jobregister) getDaoFactory().getUtilityDAO().findById(Jobregister.class,jobregisterID);
		
		JobregisterTO jobregisterTO = new JobregisterTO();
		BeanUtils.copyProperties(jobregister, jobregisterTO);
		
		if(jobregister.getContactByHandovertoid() != null){
			if(!Utils.isEmptyInteger(jobregister.getContactByHandovertoid().getContactid())){
				jobregisterTO.setHandovertoid(jobregister.getContactByHandovertoid().getContactid());
			}
		}
		
		if( jobregister.getCodetableByModeid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByModeid().getCodetableid())){
				jobregisterTO.setModeid(jobregister.getCodetableByModeid().getCodetableid());
			}
		}
		
		if( jobregister.getCodetableByCategoryid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByCategoryid().getCodetableid())){
				jobregisterTO.setCategoryid(jobregister.getCodetableByCategoryid().getCodetableid());
			}
		}
		
		if( jobregister.getPortByEmptypickupid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getPortByEmptypickupid().getPortid())){
				jobregisterTO.setEmptypickupid(jobregister.getPortByEmptypickupid().getPortid());
			}
		}
		
		if( jobregister.getCodetableByStuffpointid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByStuffpointid().getCodetableid())){
				jobregisterTO.setStuffpointid(jobregister.getCodetableByStuffpointid().getCodetableid());
			}
		}
		
		if( jobregister.getCodetableByCleranceatid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByCleranceatid().getCodetableid())){
				jobregisterTO.setCleranceatid(jobregister.getCodetableByCleranceatid().getCodetableid());
			}
		}
		
		if( jobregister.getCodetableByCargotypeid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByCargotypeid().getCodetableid())){
				jobregisterTO.setCargotypeid(jobregister.getCodetableByCargotypeid().getCodetableid());
			}
		}
		
		if( jobregister.getCodetableByAdditionalrequirementsid() != null ){
			if(!Utils.isEmptyInteger(jobregister.getCodetableByAdditionalrequirementsid().getCodetableid())){
				jobregisterTO.setAdditionalrequirementsid(jobregister.getCodetableByAdditionalrequirementsid().getCodetableid());
			}
		}
		
		jobregisterTO.setShipperid(jobregister.getContactByShipperid().getContactid());
		jobregisterTO.setBillingtoid(jobregister.getContactByBillingtoid().getContactid());
		jobregisterTO.setBookingbyid(jobregister.getContactByBookingbyid().getContactid());
		jobregisterTO.setSalespersonid(jobregister.getContactBySalespersonid().getContactid());
		jobregisterTO.setCarrierid(jobregister.getContactByCarrierid().getContactid());
		jobregisterTO.setChaid(jobregister.getContactByChaid().getContactid());
		
		jobregisterTO.setPodid(jobregister.getPortByPodid().getPortid());
		jobregisterTO.setPolid(jobregister.getPortByPolid().getPortid());
		jobregisterTO.setPfdid(jobregister.getPortByPfdid().getPortid());
		
		if(jobregister.getVoyageByBkgvslid() != null){
			if(!Utils.isEmptyInteger(jobregister.getVoyageByBkgvslid().getVoyageid())){
				jobregisterTO.setBkgvslid(jobregister.getVoyageByBkgvslid().getVoyageid());
			}
		}
		
		if( jobregister.getContactByChaid() != null ){
			jobregisterTO.setCha( jobregister.getContactByChaid().getFullname() );
		}
		else{
			jobregisterTO.setCha( "No Cha Found" );
		}
		
		return jobregisterTO; 
	}

	@Override
	@Transactional
	public List<JobregisterTO> findByAll() {
		List<Jobregister> jobregisterList = getDaoFactory().getUtilityDAO().findByAll(Jobregister.class);
		
		List<JobregisterTO> jobregisterFormList = new ArrayList<JobregisterTO>();
		
		if( jobregisterList != null && !jobregisterList.isEmpty() ){
			JobregisterTO jobregisterForm = null;
			for(Jobregister jobregister: jobregisterList){
				jobregisterForm = new JobregisterTO();
				
				BeanUtils.copyProperties(jobregister, jobregisterForm);
				if(jobregister.getCreationadate() != null){
					jobregisterForm.setCreationDateToString(Utils.convertDateObjectToDDMMYYYY(jobregister.getCreationadate()));
				}
				
				//Setting values to be displayed on UI
				jobregisterForm.setCategory(jobregister.getCodetableByCategoryid().getFormvalue());
				jobregisterForm.setEmptyPickup(jobregister.getPortByEmptypickupid().getPortname());
				jobregisterForm.setCargoType(jobregister.getCodetableByCargotypeid().getFormvalue());
				jobregisterForm.setStuffPoint(jobregister.getCodetableByStuffpointid().getFormvalue());
				jobregisterForm.setClearanceAt(jobregister.getCodetableByCleranceatid().getFormvalue());
				
				jobregisterFormList.add(jobregisterForm);
			}
		}	
		return jobregisterFormList;
	}

	@Override
	@Transactional
	public List<DataHolderTO> populateJobs(String jobno) {
		List<Jobregister> jobregisterList = getDaoFactory().getUtilityDAO().findByCondition(Jobregister.class, Utils.getLikeExpressionList("jobno", jobno));
		
		List<DataHolderTO> jobRegisterResultList = new ArrayList<DataHolderTO>();
		if( jobregisterList != null && !jobregisterList.isEmpty() ){
			DataHolderTO dataHolderTO = null;
			for(Jobregister jobregister: jobregisterList){
				dataHolderTO = new DataHolderTO();
				
				dataHolderTO.setFieldId(jobregister.getJobnoid());
				dataHolderTO.setFieldname(jobregister.getJobno());
				
				jobRegisterResultList.add(dataHolderTO);
			}
		}
		
		return jobRegisterResultList;
	}
}